package com.vnt.controller;


import com.vnt.common.Result;
import com.vnt.entity.Order;
import com.vnt.feign.client.UserClient;
import com.vnt.feign.entity.Role;
import com.vnt.feign.entity.User;
import com.vnt.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class Controller {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private UserClient userClient;

    @GetMapping("/")
    public String hello(){
        return "order";
    }

    @GetMapping("/getById/{id}")
    public Order getById(@PathVariable("id") Long id){
        Order order = new Order();
        User user = userClient.getById(id);
        order.setLabel("new Label"+id);
        order.setUser(user);
        return order;
    }

    @GetMapping("/list")
    public Result<?> roleList(){
        List<Map<String, Object>> list = sysRoleService.getOptions();
        System.out.println("getOptions");
        System.out.println(list);
        return Result.success(list);
    }

    @GetMapping("/pages/list")
    public Result<?> test(){
        List<Role> list = sysRoleService.list();
        return Result.success(list);
    }

    @PostMapping("/pages/insert")
    public Result<?> insert(@RequestBody Role sysRole){
        if(sysRole==null){
            return Result.error("-1","数据无效");
        }
        boolean b = sysRoleService.save(sysRole);
        return Result.success("ok");
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/pages/selectById/{id}")
    public Result<?> selectById(@PathVariable Long id){
        System.out.println("id:"+id);
        return Result.success(sysRoleService.getById(id));
    }
    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/pages/deleteById/{id}")
    public Result<?> deleteById(@PathVariable Long id){
        boolean b = sysRoleService.removeById(id);
        System.out.print("delete byId:"+id);
        if(!b){
            return Result.error("-1","删除失败,原因未知");
        }
        return Result.success("删除成功");
    }

    /**
     * 更新角色
     */
    @PostMapping("/pages/update")
    @Transactional
    public Result<?> update(@RequestBody Role sysRole){
        System.out.println("修改用户: "+sysRole.getId());
        System.out.println(sysRole.getKeyName());
        if(sysRole==null){
            return Result.error("-1","数据无效");
        }
        return Result.success(sysRoleService.updateById(sysRole));
    }
}
